<?php
class grade extends core {
	public function __construct() {
		parent::__construct();
	}

	//index
	public function index() {
		$where = '';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		if ($keyword) {
			$where .= "AND (name LIKE '%{$keyword}%' OR keywords LIKE '%{$keyword}%')";
		}
		if ($id) {
			$where .= "AND id='{$id}'";
		}
		//分页
		$this->setpages(array('id'=>$id, 'keyword'=>$keyword));

		$sql = "SELECT * FROM jf_member_grade WHERE 1 {$where} ORDER BY id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);

		$this->smarty->assign('is_admin', $_SESSION['admin']->is_admin);
		$this->display();
	}

	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$sql = "SELECT * FROM jf_member_grade WHERE 1 ORDER BY  id DESC";
			$row = $this->db->get_results($sql);
			$this->smarty->assign('row', $row);
			$this->display();
		}
	}	

	//add
	public function edit() {
		if (IS_POST) { //添加
			$this->save();
		} else { //显示
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM jf_member_grade WHERE id='{$id}'";
			$row = $this->db->get_row($sql);
			$sql = "SELECT * FROM jf_member_grade WHERE 1 ORDER BY  id DESC";
			$rs = $this->db->get_results($sql);
			$this->smarty->assign('rs', $rs);
			$this->smarty->assign('row', $row);
			$this->display();
		}
	}
	
	//写入到数据库
	public function save() {
		$id = (isset($_POST['id']) && trim($_POST['id'])) ? intval($_POST['id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : 0;
		$memo = (isset($_POST['memo']) && trim($_POST['memo'])) ? trim($_POST['memo']) : '';
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$sort = (isset($_POST['sort']) && intval($_POST['sort'])) ? intval($_POST['sort']) : 0;
		$pic = upload_one_file('pic','pic');
		if ($id > 0) {  //edit
			$sql = "UPDATE jf_member_grade SET name='{$name}', memo='{$memo}',status='{$status}', sort='{$sort}'
				WHERE id='{$id}'";
			$this->db->query($sql);
			//是否更改图片
			if($pic){
				$sql = "UPDATE jf_member_grade SET pic= '{$pic}' WHERE id='{$id}'";
				$this->db->query($sql);
			}
		} else { //add
			$sql = "INSERT INTO jf_member_grade(name, memo, status, sort ,pic ) 
				VALUES('{$name}', '{$memo}', '{$status}', '{$sort}' ,'{$pic}' )";
			$this->db->query($sql);
			$id = $this->db->insert_id;
			if($id){
				echo"<meta charset='UTF-8'><script>alert('添加成功');window.location.href='?app=grade&act=index';</script>";
			}else{
				echo"<meta charset='UTF-8'><script>alert('添加失败');window.location.href='?app=grade&act=index';</script>";
			}
		}
		header("Location:?app=grade&act=index");
	}

	private function _get_upload_pics($html) {
		if ($html) {
			$html = stripcslashes($html);
			//<p><img alt="" src="http://xfb/uploadfiles/editor/1yuan.jpg" style="height:230px; width:620px" /></p>
			if (preg_match_all('/(\/uploadfiles\/article\/(.*?))"/i', $html, $matches)) {
				return $matches[1];
			}
		}
	}

	//delete
	public function delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM jf_member_grade WHERE id='{$id}'";
		$this->db->query($sql);
		//exit;
		header("Location:?app=grade&act=index");
	}
}
